const http = require("../../utils/http.js")
let inter;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPhone: false,
        formData: {
            phone: "",
            code: "",
            uuid: "",
            jsCode: ""
        },
        showDialog: false,
        kaptchaSrc: "",
        uuid: "",
        code: "",
        btnMsg: "发送验证码",
        beforeClose(action) {
            return action !== "confirm";
        }
    },
    wxLogin() {
        wx.login({
            success(res) {
                http.get("wx/v1/wx/login", {jsCode: res.code}).then(data => {
                    wx.setStorageSync("wxToken", data)
                    wx.showToast({title: "登录成功", icon: "success"})
                    wx.reLaunch({url: "/pages/home/home"})
                })
            }
        })
    },
    phoneLogin() {
        this.setData({
            isPhone: true
        })
    },
    getPhone(e) {
        this.setData({
            "formData.phone": e.detail
        })
    },
    getCode(e) {
        this.setData({
            "formData.code": e.detail
        })
    },
    sendMsg() {
        if (!this.data.formData.phone) {
            wx.showToast({title: "手机号为空！", icon: "error"});
            return false;
        }
        if (!(/^1[0-9]{10}$/.test(this.data.formData.phone))) {
            wx.showToast({title: "手机号错误！", icon: "error"});
            return false;
        }
        this.setData({
            showDialog: true,
        }, () => {
            setTimeout(() => {
                this.getKaptcha()
            }, 200)
        })
    },
    getKaptcha() {
        const uuid = new Date().getTime();
        this.setData({
            uuid,
            kaptchaSrc: http.baseUrl + "kaptcha.jpg?uuid=" + uuid
        })
    },
    getKaptchaCode(e) {
        this.setData({
            code: e.detail.value
        })
    },
    dialogClose() {
        this.setData({
            uuid: "",
            code: "",
        })
    },
    dialogConfirm() {
        if (!this.data.code) {
            wx.showToast({title: "验证码为空！", icon: "error"});
            return;
        }
        http.get('wx/v1/send/msm', {
            uuid: this.data.uuid,
            code: this.data.code,
            phone: this.data.formData.phone
        }).then(data => {
            wx.showToast({title: "发送成功！", icon: "success"})
            this.setData({
                "formData.uuid": data,
                showDialog: false,
                btnMsg: "60秒后重试"
            })
            let s = 60;
            inter = setInterval(() => {
                s--;
                this.setData({
                    btnMsg: (s < 10 ? "0" + s : s) + "秒后重试"
                })
                if (s === 0) {
                    clearInterval(inter);
                    this.setData({
                        btnMsg: "发送验证码"
                    })
                }
            }, 1000);

        })
    },
    login() {
        if (!this.data.formData.phone) {
            wx.showToast({title: "手机号为空！", icon: "error"});
            return false;
        }
        if (!(/^1[0-9]{10}$/.test(this.data.formData.phone))) {
            wx.showToast({title: "手机号错误！", icon: "error"});
            return false;
        }
        if (!this.data.formData.code) {
            wx.showToast({title: "验证码为空！", icon: "error"});
            return false;
        }
        this.data.formData.type = 2;//这种方式不会触发页面渲染
        http.post('wx/v1/phone/login', this.data.formData).then(data => {
            wx.setStorageSync("wxToken", data)
            wx.showToast({title: "登录成功", icon: "success"})
            wx.reLaunch({url: "/pages/home/home"})
        })
    },
    back() {
        this.setData({
            uuid: "",
            code: "",
            btnMsg: "发送验证码",
            isPhone: false
        })
        clearInterval(inter)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
